package cn.wolfcode.Utils;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

//获取实体类属性 封装到MAP 返回JSON
public class JsonUtil {

    public static String convert(Object obj,Class clzz,String format)throws Exception {
        Map<String, Object> map = new HashMap<>();
        Field[] fields = clzz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);

            if (field.getType().equals(Date.class)) {
                String sdate = DateUtil.format((Date) field.get(obj), format);
                map.put(field.getName(), sdate);
            } else {
                map.put(field.getName(), field.get(obj));
            }
        }
        return JSON.toJSONString(map);
    }

}
